Service accounts belong
to your application or VM instance instead of to an individual user. They're used by your application
to call the Google API or service so that users aren't directly
involved in the authentication process. Each service account is identified
by its unique e-mail address. Service accounts are associated
with a key pair and can have up to 10 key pairs associated with them
to facilitate key rotation, which is managed by Google
and done daily. Service accounts enable
authentication and authorization because you can assign specific
IAM roles to a service account. You can create external keys
to be used from outside GCP. They require that you manage
the security of the private key and perform management operations
such as key rotation. External keys are manageable
through the IAM API, Gcloud and the GCP Console. You can optionally use
API keys to call certain APIs that don't need to access
private user data. API keys are useful in clients
such as browser and mobile applications that don't have a back-end server.
The API key is used to track API requests associated with your project
for quota and billing. You can define many nondefault
service accounts in a project. The diagram shows a use case
to manage many microservices in a single cloud project
by letting each microservice be represented
by its own service account. Granularity is employed
by specifying which service accounts are allowed
to call which other services. To use a service account
in your application, first create the service account
via the console. You then generate and download
your credentials file, and then you can set
an environment variable with the path
to your downloaded credentials. The example shows commands
to set the environment variable in both Linux, OSX and Windows. You can then make an authenticated
API request in your code. The example code is in Python
and shows that if you don't
explicitly specify credentials when constructing a client, the client library will look
for credentials in the environment. Google uses credentials to identify
your application for quota and billing and to authorize access to GCP
APIs, resources and features. GCP client libraries use
application default credentials, or ADC,
to find your application's credentials. Credentials are checked for
in the following order. ADC will check
for the GOOGLE_APPLICATION_CREDENTIALS environment variable.
If the environment variable is set, ADC will use the service account file
that the variable points to. If the environment
variable isn't set, ADC will use the default service account
that Compute Engine, Google Kubernetes Engine,
App Engine and Cloud Functions provide. Assuming that your application
runs on those services. If neither
the environment variable nor the default service accounts
can be found an error will occur. The example demonstrates using Python
to implicitly find the credentials, assuming that either
the environment variable is set or the application
is running on Compute Engine, Google Kubernetes Engine,
App Engine or Cloud Functions. Remember the following
best practices. When using cloud IAM,
follow the principle of least privilege. Use predefined roles,
more granular access when possible. Grant predefined roles
to identities when possible. So you only give the least
amount of access necessary to access your resources. Grant roles at
the smallest scope needed. Restrict who can create and manage
service accounts in your project. Rotate service account keys
regularly. And implement processes to manage
user managed service account keys. Don't check in service
account keys to source code. Use Cloud Audit Logging to regularly
audit changes to your IAM policy and export logs
to Cloud Storage. Set organization-level
IAM policies to grant access to all projects in your organization
and grant roles to a Google group instead of to individual users
when possible.